package com.wss.lsl.lucene.demo.sort;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.document.NumericField;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;

public class LuceneSort {
	
	private static Directory directory;
	private static IndexWriter writer;
	private static IndexReader reader;
	private static IndexSearcher searcher;
	private static Analyzer analyzer = new IKAnalyzer();
	private static final String COUNT = "count";
	static{
		try {
			directory = FSDirectory.open(new File("d:\\index_sort"));
			IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_35, analyzer);
			writer = new IndexWriter(directory, config);
			initIndex();
			reader = IndexReader.open(directory);
			searcher = new IndexSearcher(reader);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static void initIndex(){
		try {
			writer.deleteAll();
			
			Document doc1 = new Document();
			doc1.add(new Field("content", "like", Field.Store.YES, Field.Index.NOT_ANALYZED));
			doc1.add(new NumericField(COUNT, Field.Store.YES, true).setIntValue(3));
			writer.addDocument(doc1);
			
			doc1 = new Document();
			doc1.add(new Field("content", "look look", Field.Store.YES, Field.Index.NOT_ANALYZED));
			doc1.add(new NumericField(COUNT, Field.Store.YES, true).setIntValue(4));
			writer.addDocument(doc1);
			
			writer.close();
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static IndexSearcher getSearcher(){
		try {
			IndexReader reader = searcher.getIndexReader();
			IndexReader tr = IndexReader.openIfChanged(reader);
			if(tr != null){
				reader = tr;
				searcher = new IndexSearcher(reader);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return searcher;
	}
	
	public static void searchByWildcardQuery(String field, String value, int num){
		try {
			WildcardQuery query = new WildcardQuery(new Term(field, value + "*"));
			searcher.setDefaultFieldSortScoring(true, true);
			TopDocs hits = searcher.search(query, num);
			int count = hits.totalHits;
			System.out.println("Found " + count + " hits!");
			StringBuilder sb = new StringBuilder();
			for(ScoreDoc hit : hits.scoreDocs){
				sb.setLength(0);
				Document doc = searcher.doc(hit.doc);
				List<Fieldable> fieldables = doc.getFields();
				for(Fieldable fieldable : fieldables){
					sb.append(fieldable.name()+ ": " + fieldable.stringValue() + "\t");
				}
				System.out.println(sb.toString());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void searchByWildcardQuery2(String field, String value, int num){
		try {
			WildcardQuery query = new WildcardQuery(new Term(field, value + "*"));
			SortField sortField = new SortField(COUNT, SortField.INT, true);
			Sort sort = new Sort(sortField);
			TopDocs hits = searcher.search(query, num, sort);
			int count = hits.totalHits;
			System.out.println("Found " + count + " hits!");
			StringBuilder sb = new StringBuilder();
			for(ScoreDoc hit : hits.scoreDocs){
				sb.setLength(0);
				Document doc = searcher.doc(hit.doc);
				List<Fieldable> fieldables = doc.getFields();
				for(Fieldable fieldable : fieldables){
					sb.append(fieldable.name()+ ": " + fieldable.stringValue() + "\t");
				}
				System.out.println(sb.toString());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
